//
//  CTPLoginViewController.m
//  LittleNotes
//
//  Created by 檀志文 on 16/5/19.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "CTPLoginViewController.h"
#import <Masonry.h>
#import "MBProgressHUD+PKX.h"
#import "CTPNavViewController.h"
#import "CTPDetailListViewController.h"
#import "AppDelegate.h"


@interface CTPLoginViewController ()
//用户名
@property (weak, nonatomic) IBOutlet UITextField *txtUserName;
//密码
@property (weak, nonatomic) IBOutlet UITextField *txtPwd;

//登录按钮
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
//logo
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end

@implementation CTPLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//  一进来默认加载用户的信息
    [self loadUserInfo];
    
//  设置圆角
    self.iconView.layer.cornerRadius = 50;
    self.iconView.layer.masksToBounds =YES;

}
//加载用户的信息
-(void)loadUserInfo{
    [self settxtStyle:@"请输入用户名" imageName:@"login_rigisterUserName" textField:_txtUserName];
    [self settxtStyle:@"请输入密码" imageName:@"login_locked" textField:_txtPwd];
    self.txtPwd.secureTextEntry =YES;

}

// 输入文本框的内容

-(void) settxtStyle:(NSString *)placeholder imageName:(NSString *)imageName textField:(UITextField *)textField{
    textField.placeholder = placeholder;
    
    textField.layer.cornerRadius = 5;
    textField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    textField.layer.borderWidth = 0.5;
    textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView* imgUser = [[UIImageView alloc] initWithFrame:CGRectMake(11, 11, 22, 22)];
    imgUser.image = [UIImage imageNamed:imageName];
    [textField.leftView addSubview:imgUser];
    
}
// 登录
- (IBAction)login:(UIButton *)sender {
    // 隐藏键盘
    [self.view endEditing:YES];
    // 获得账号
    NSString *account = self.txtUserName.text;
    if (![account isEqualToString:@"123"]) {
        [MBProgressHUD showError:@"账号错误"];
        return;
    }
    // 获得密码
    NSString *password = self.txtPwd.text;
    if (![password isEqualToString:@"123"]) {
        [MBProgressHUD showError:@"密码错误"];
        return;
    }
    
    // 模拟延时
    [MBProgressHUD showMessage:@"正在努力登录中..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 隐藏登录指示器
        [MBProgressHUD hideHUD];
        // 提示登录成功
        [MBProgressHUD showSuccess:@"登录成功"];
        // 保存用户的登录信息
        [self saveUserInfo];
//      跳转到详细列表
        #warning 跳转
        
        CTPDetailListViewController *detailList = [[CTPDetailListViewController alloc]init];
        
       
        CTPNavViewController *nav = [[CTPNavViewController alloc]initWithRootViewController:detailList];
        
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        
        [app switchViewController:nav];
//        [self presentViewController:nav animated:YES completion:nil];
//        [self.navigationController pushViewController:nav animated:YES];
        
    });
}

#pragma mark  保存/读取用户信息
/**
 *  保存
 */
- (void)saveUserInfo {
    // 获得偏好设置对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.txtUserName.text forKey:CTPAccount];
    [defaults setObject:self.txtPwd.text forKey:CTPPassword];
    // 立即同步
    [defaults synchronize];
}

/**
 *  读取
 */
- (void)getUserInfo {
    // 获得偏好设置对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.txtUserName.text = [defaults objectForKey:CTPAccount];
    self.txtPwd.text = [defaults objectForKey:CTPPassword];
}

#warning --纯代码的方式到时候调ui
////  按钮设置
////设置按钮的  形状
//UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//self.loginBtn = loginBtn;
//[self.view addSubview:loginBtn];
//[self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
//
//[self.loginBtn setBackgroundColor:CMTGlobalColor];


@end
